package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
//import com.atguigu.jxc.entity.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        PageHelper.startPage(page,rows);
        Map<String,Object> map = new HashMap<>();
        Page<Goods> page1 = goodsDao.listInventory(page,rows,codeOrName,goodsTypeId);
        map.put("total",page1.getTotal());
        map.put("rows",page1.getResult());
             return map;
    }

    @Override
    //未添加库存的商品展示getNoInventoryQuantity
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows,
                                                      String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        //开启分页
            PageHelper.startPage(page,rows);
            Page<Goods> page2 = goodsDao.getNoInventoryQuantity(nameOrCode);
            map.put("total",page2.getTotal());
            map.put("rows",page2.getResult());
            return map;


    }

    @Override
    //有库存商品展示
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        //开启分页
        PageHelper.startPage(page,rows);
        Page<Goods> page1 = goodsDao.getHasInventoryQuantity(nameOrCode);
        map.put("total",page1.getTotal());
        map.put("rows",page1.getResult());
        return map;
    }

    @Override
    //修改价格,库存数
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {

        goodsDao.updatePriceAndQuantity(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public Map<String, Object> listSelectAllGoods(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        //开启分页
        PageHelper.startPage(page,rows);
        Page<Goods> page1 = goodsDao.listSelectAllGoods(goodsName,goodsTypeId);
        map.put("total",page1.getTotal());
        map.put("rows",page1.getResult());
        return map;
    }

    @Override
    public void saveOrUpdateGoods(Goods goods) {

        if ( goods.getGoodsId()==null){
            goodsDao.saveGoods(goods);

        }else{
            goodsDao.updateGoods(goods);
        }

    }

    @Override
    //删除商品
    public ServiceVO deleteGoods(Integer goodsId) {

        Goods goods = goodsDao.getGoodsById(goodsId);

        if (goods.getState() == 1 && goods.getState() == 2){
            return new ServiceVO(10314,"该商品有进货和销售单据的不能删除库存");
        }else{
            goodsDao.deleteGoods(goodsId);
            return new ServiceVO(10340,"请求成功");
        }

    }

}



